<?php
/**
* @desc model层基类
* @author kevin.wang
* @date 2015-01-01 12:00:00
**/
class Base extends CActiveRecord
{
	const QUERY_ALL = "all";
	const QUERY_RAW = "raw";
	const QUERY_COLUMN = "column";
	//设置数据表
	public function tableName(){
		return $this->table;
	}
	//查询数据表
	function select_table($sql, $binds, $type = Base::QUERY_ALL){
		$result = array();
		try{
			$con = Yii::app()->db;
			$stmt = $con->createCommand($sql);
			$stmt->execute($binds);
			switch($type){
				case Base::QUERY_ALL:$result = $stmt->queryAll();break;
				case Base::QUERY_RAW:$result = $stmt->queryRow();break;
				case Base::QUERY_COLUMN:$result = $stmt->queryColumn();break;
				default:$result = $stmt->queryAll();break;
			}
		}catch(Exception $e){
			$code = $e->getCode();
			$msg = $e->getMessage();
			$bind = json_encode($binds);
			$message = "{$code}\t{$msg}\t{$sql}\t{$bind}";
			Yii::log($message, CLogger::LEVEL_ERROR, "models.base.error");
		}
		return $result;
	}
	//修改数据表
	function update_table($sql, $binds){
		$result = 0;
		try{
			$con = Yii::app()->db;
			$stmt = $con->createCommand($sql);
			$result = $stmt->execute($binds);
		}catch(Exception $e){
			$code = $e->getCode();
			$msg = $e->getMessage();
			$bind = json_encode($binds);
			$message = "{$code}\t{$msg}\t{$sql}\t{$bind}";
			Yii::log($message, CLogger::LEVEL_ERROR, "models.base.error");
		}
		return $result;
	}
	//写入数据表
	function insert_table(array $data, $table){
		$result = 0;
		try{
			if(empty($data) || empty($table)){
				throw new Exception("data is error");
			}
			$binds = array();
			$sql = "insert into `{$table}` set";
			foreach($data as $key => $value){
				$binds[":{$key}"] = $value;
				$sql .= "`{$key}` = :{$key},";
			}
			$sql .= "`raw_add_time` = NOW()";
			$sql = trim($sql, ",");
			$con = Yii::app()->db;
			$stmt = $con->createCommand($sql);
			$result = $stmt->execute($binds);
		}catch(Exception $e){
			$code = $e->getCode();
			$msg = $e->getMessage();
			$bind = json_encode($binds);
			$message = "{$code}\t{$msg}\t{$sql}\t{$bind}";
			Yii::log($message, CLogger::LEVEL_ERROR, "models.base.error");
		}
		return $result;
	}
	//数据事务操作
	function transaction(array $sqls, array $binds){
		$result = false;
		try{
			if(empty($sqls) || empty($binds)){
				throw new Exception("data is error");
			}
			$con = Yii::app()->db;
			$trans = $con->beginTransaction();
			foreach($sqls as $key => $value){
				$sql = $value;
				$bind = $binds[$key];
				$stmt = $con->createCommand($sql);
				$rawCount = $stmt->execute($bind);
				if(empty($rawCount)){
					throw new Exception("transation is fail");
				}
			}
			$trans->commit();
			$result = true;
		}catch(Exception $e){
			$trans->rollBack();
			$code = $e->getCode();
			$msg = $e->getMessage();
			$bind = json_encode($bind);
			$message = "{$code}\t{$msg}\t{$sql}\t{$bind}";
			Yii::log($message, CLogger::LEVEL_ERROR, "models.base.error");
		}
		return $result;
	}
	//组装展示文字
	protected function hotWords($keywords){
		$arr = json_decode($keywords, true);
		if(empty($arr)){
			return "";
		}
		$len = count($arr);
		if($len == 2){
			return "<strong>{$arr[0]}</strong>
                    <span>{$arr[1]}</span>";
		}else{
			return "<span>{$arr[0]}</span>
                    <strong>{$arr[1]}</strong>
                    <span>{$arr[2]}</span>";
		}
	}
}
